#include <cubefs/api/api.hpp>
#include <natives.h>
#include <decls.hpp>
#include <exceptions.hpp>

#include <cubefs/core/tools/types/dispatcher.hpp>

#define CUBEFS_SOURCE "string.cpp:" CUBEFS_AT

using namespace cubefs::api;
using namespace cubefs::tools;

using namespace std;

JNIEXPORT jlong JNICALL METADATA1(StringValue, newValue) (JNIEnv *env, jclass) {
    CUBEFS_EXC_BEGIN
    return Ptr2Handle(new TypedValue<String>());
    CUBEFS_EXC_HANDLE
    return 0;
}

JNIEXPORT jstring JNICALL METADATA1(StringValue, getValue) (JNIEnv *env, jclass, jlong handle) {
    CUBEFS_EXC_BEGIN

    TypedValue<String> *value = Handle2Ptr<TypedValue<String> >(handle);
    return env->NewStringUTF(value->getValue().c_str());

    CUBEFS_EXC_HANDLE
    return NULL;
}

JNIEXPORT void JNICALL METADATA1(StringValue, setValue) (JNIEnv *env, jclass, jlong handle, jstring v) {
    CUBEFS_EXC_BEGIN

    TypedValue<String> *value = Handle2Ptr<TypedValue<String> >(handle);

    const char *str = env->GetStringUTFChars(v, NULL);
    if (str == NULL) {
        return; /* OutOfMemoryError already thrown */
    }

    value->setValue(str);

    env->ReleaseStringUTFChars(v, str);

    CUBEFS_EXC_HANDLE
}


JNIEXPORT jstring JNICALL METADATA1(StringRef, getValue) (JNIEnv *env, jclass, jlong handle) {
    CUBEFS_EXC_BEGIN

    ConstTypedValue<StringRef>* value = Handle2Ptr<ConstTypedValue<StringRef> >(handle);
    return env->NewStringUTF(value->getValue().c_str());

    CUBEFS_EXC_HANDLE
    return NULL;
}


#undef CUBEFS_SOURCE
